/*
** client.c for my_irc in /home/billey_b/projet/my_irc/my-irc-a
** 
** Made by Bastien Billey
** Login   <billey_b@epitech.net>
** 
** Started on  Tue Apr 17 11:24:19 2012 Bastien Billey
** Last update Tue Apr 17 11:28:20 2012 Bastien Billey
*/

#include <sys/types.h>
#include <sys/socket.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <netdb.h>
#include <netinet/in.h>
#include <stdio.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <signal.h>
#include "client.h"
#include "ptr.h"

void	choose_func(t_client *client)
{
  int	i;

  i = 0;
  gets(client->cmd.buff);
  client->cmd.commande = parse_cmd(client->cmd.buff);
  if (client->cmd.commande[0] != '/')
    send_msg(client);
  else
    while (ptr_tab[i].func_name != NULL)
      {
	if (strcmp(ptr_tab[i].func_name, client->cmd.commande) == 0)
	  {
	    ptr_tab[i].ptr_func(client);
	    break;
	  }
	i++;
      }
}

static void	exec(t_client *client)
{
  char	buff[1024];
  fd_set fd_read;

  client->cmd.buff = malloc(1024);
  client->cmd.commande = malloc(1024);
  while (42)
    {
      printf(">>");
      fflush(stdout);
      FD_ZERO(&fd_read);
      FD_SET(0, &fd_read);
      FD_SET(sock_client, &fd_read);
      xselect(sock_client + 1, &fd_read, NULL, NULL, NULL);
      if (FD_ISSET(0, &fd_read))
	choose_func(client);
      else if (FD_ISSET(sock_client, &fd_read))
	{
	  recv(sock_client, buff, sizeof(buff), 0);
	  printf("\n%s\n", buff);
	}
    }
}

int	main(int ac, char **av)
{
  t_client	client;

  exec(&client);
  close(sock_client);
}
